package com.example.administrator.mymusic;

import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;

import com.example.administrator.mymusic.Class.SongsClass;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private Intent intent;
    private List<SongsClass> datas;
    private MyService myService;
    private XiaFragment xiaFragment;
    private ZhongFragment zhongFragment;
    private int playSeek;
    public static int sXunhuan = 0;
    public static int[] sXunHuanIM = {R.mipmap.repeat, R.mipmap.repeat_press, R.mipmap.shuffl};
    private SharedPreferences sharedPreferences;

    public int getPlaySeek() {
        return playSeek;
    }

    public MyService getMyService() {
        return myService;
    }

    public List<SongsClass> getDatas() {
        return datas;
    }

    public ZhongFragment getZhongFragment() {
        return zhongFragment;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getsongs();
        sharedPreferences = getSharedPreferences("app", MODE_PRIVATE);
        sXunhuan = sharedPreferences.getInt("sXunhuan", 0);
        sXunhuan = sharedPreferences.getInt("sXunhuanIm", R.mipmap.repeat);


        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        zhongFragment = new ZhongFragment();
        xiaFragment = new XiaFragment();
        ShangFragment shangFragment = new ShangFragment();
        transaction.replace(R.id.ll_main_shang, shangFragment, "shangFragment");
        transaction.replace(R.id.ll_main_zhong, zhongFragment, "zhongFragment");
        transaction.replace(R.id.ll_main_xia, xiaFragment, "xiaFragment");
        transaction.commit();
//       LrcFragment lrcFragment= (LrcFragment) zhongFragment.getFragmentList().get(1);
        intent = new Intent(MainActivity.this, MyService.class);
//        intent.putExtra("");
        intent.putExtra("songs", (Serializable) datas);
        bindService(intent, connection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        xiaFragment.songsTv.setText(datas.get(0).getName());

    }

    public void getsongs() {
        datas = new ArrayList<>();
        ContentResolver resolver = getContentResolver();
        //内容提供者访问MediaStore.Audio.Media.EXTERNAL_CONTENT_URI这个地址获取系统音乐文件详细信息。
        Cursor c = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        while (c.moveToNext()) {
            //音乐id
            long id = c.getLong(c.getColumnIndex(MediaStore.Audio.Media._ID));
            //名字
            String name = c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));

            //专辑名
            String album = c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
            int album_id = c.getInt(c.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
            //歌手名
            String artist = c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
            //URI 歌曲文件存放路径
            String path = c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
            //歌曲文件播放时间长度
            int duration = c.getInt(c.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
            //音乐文件大小
            int size = c.getInt(c.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
//            Bitmap bu=c.getBlob(c.getColumnIndexOrThrow(MediaStore.Images.ImageColumns));
            int isMusic = c.getInt(c.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)); // 是否为音乐
//            Log.e("5555555", ""+duration);
            if (duration >= 180000) {
                datas.add(new SongsClass(id, name, album, album_id, artist, path, duration, size));

            }

        }
    }


    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            SongsClass aClass = (SongsClass) msg.getData().get("songs");
            xiaFragment.songsTv.setText(aClass.getName());
            xiaFragment.songsSb.setMax(myService.maxSeekBar);
            playSeek = msg.getData().getInt("seekbar");
            xiaFragment.songsSb.setProgress(playSeek);
            return false;
        }
    });
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MyService.MyBinder myBinder = (MyService.MyBinder) service;
            myService = myBinder.getMyService();
            myService.setCillBack(new MyService.CillBack() {
                @Override
                public void onDataChange(Bundle bundle) {
                    Message mes = new Message();
                    mes.setData(bundle);
                    handler.sendMessage(mes);
                }
            });
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
            return true;
        }
        return super.onKeyDown(keyCode, event);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
        stopService(intent);


    }
}
